﻿using System;
using System.Text;
using System.Data;

using ITViet.Web.App_Code;
using ITViet.Domains;
using ITViet.Services;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace ITViet.Web.Pages
{
	public partial class ProductDetail : BasePage
	{
		#region Properties
		public string ProductName
		{
			get;
			set;
		}
		#endregion

		#region Events
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack && !IsCallback)
			{
				if (Id == 0)
				{
					TList<Product> productList = new ProductService().GetAll();
					if (productList.Count > 0)
					{
						Response.Redirect(string.Format("ProductDetail.aspx?tid={0}&id={1}", productList[0].CategoryId.ToString(), productList[0].Id.ToString()));
					}
				}

				LoadData();
			}
		}
		#endregion

		#region Methods
		public string TextToHtml(string text, int length)
		{
			string result = string.Empty;
			if (!string.IsNullOrEmpty(text))
			{
				if (text.Length > length)
				{
					result = text.Substring(0, length) + "...";
				}
				else
				{
					result = text;
				}

				result = result.Replace("\n", "<br>");
			}

			return result;
		}

		private void LoadData()
		{
			DataSet ds = new ProductLocaleService().GetAllInfoByProductId(CurrentLanguage.Id, Id);
			if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
			{
				DataRow row = ds.Tables[0].Rows[0];
				if (row != null)
				{
					#region Info
					ProductName = row["Name"] == DBNull.Value ? string.Empty : row["Name"].ToString();
					Literal lrPageTitle = Master.FindControl("lrPageTitle") as Literal;
					if (lrPageTitle != null)
					{
						lrPageTitle.Text = ProductName;
					}

					bool? isHot = row["IsHot"] == DBNull.Value ? null : row["IsHot"] as bool?;
					if (isHot.HasValue && isHot.Value)
					{
						imgHot.Style["Display"] = string.Empty;
					}

					divSummary.InnerHtml = TextToHtml(row["Description"] == DBNull.Value ? string.Empty : row["Description"].ToString(), int.MaxValue);

					string refPrice = row["RefPrice"] == DBNull.Value ? "0" : ((decimal)row["RefPrice"]).ToString("#,###.##");
					spanRefPrice.InnerHtml = refPrice + " " + Resources.Strings.CurrencyUnit;

					ltrContent.Text = row["Content"] == DBNull.Value ? string.Empty : row["Content"].ToString();
					#endregion

					#region Photo
					string image = row["Image"] == DBNull.Value ? string.Empty : row["Image"].ToString();
					imgPreview.Src = string.IsNullOrEmpty(image) ? string.Format("../{0}", FolderDirector.PreviewImage) : string.Format("../{0}", image);

					int? albumId = row["AlbumId"] == DBNull.Value ? null : row["AlbumId"] as int?;
					if (albumId.HasValue)
					{
						TList<Multimedia> mediaList = new MultimediaService().GetByAlbumId(albumId);
						if (!string.IsNullOrEmpty(image))
						{
							Multimedia media = new Multimedia();
							media.Path = image;
							mediaList.Insert(0, media);
						}

						rptProductImage.DataSource = mediaList;
						rptProductImage.DataBind();
					}
					#endregion
				}
			}
		}
		#endregion
	}
}
